/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cs425proj.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author rakesh reddy
 */
public class addComment extends HttpServlet {
     private Connection con ;
     private Statement stm;
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        try {
            
            
            
         this.startConnection();
           String mname=request.getParameter("mname");
           String tname=request.getParameter("tname");
           String uname=request.getParameter("uname");
           String cont=request.getParameter("cont");
           String title=request.getParameter("title");
           
           int dis_id=Integer.parseInt(request.getParameter("dis_id")); 
           int memid=Integer.parseInt(request.getParameter("memid"));
           int vis=Integer.parseInt(request.getParameter("vis")); 
           String comm=request.getParameter("comm");
           
           
           System.out.println("uname:" +uname);
           
           String qry = "insert into discussion_reply values(seq_reply_id.nextval ,?,?,?)";
           PreparedStatement pstmt = con.prepareStatement(qry);
            
            pstmt.setInt(1, dis_id);
            pstmt.setString(2,comm);
            pstmt.setInt(3,memid);
            pstmt.executeUpdate(); 
            /*
             * TODO output your page here. You may use following sample code.
             */
           
           
           
           
           
           request.setAttribute("mname", mname);
                request.setAttribute("uname", uname);
                request.setAttribute("cont", cont);
                request.setAttribute("tname", tname);
                request.setAttribute("title", title);
                request.setAttribute("vis", vis);
                request.setAttribute("dis_id", dis_id);
                request.setAttribute("memid", memid);
             String destination="/printDiscussion.jsp";
             RequestDispatcher rd = getServletContext().getRequestDispatcher(destination);
             rd.forward(request, response); 
           
//            out.println("<html>");
//            out.println("<head>");
//            out.println("<title>Servlet addComment</title>");            
//            out.println("</head>");
//            out.println("<body>");
//            out.println("<h1>Servlet addComment at " + request.getContextPath() + "</h1>");
//            out.println("</body>");
//            out.println("</html>");
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(addComment.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(addComment.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

public void startConnection(){
        
       String connStr ="jdbc:oracle:thin:@fourier.cs.iit.edu:1521:orcl";
       
       
       
           try{
               //load the drives
            Class.forName("oracle.jdbc.driver.OracleDriver");

            //initialize the connection variable
               con = DriverManager.getConnection(connStr, "rjairaj", "cs425dbo");
                     
                      
             }catch(SQLException e){e.printStackTrace();}
              catch(ClassNotFoundException cn){cn.printStackTrace();}
           
           }
    
    public void closeConnection(){
           try{
            con.commit();
            con.close();
    
           }catch(SQLException e){e.printStackTrace();}
    }
}
